import qs from 'qs'
import axios from 'axios'
import cookies from 'js-cookie'
import Url from 'url-parse'
import {Toast} from 'vant'

axios.interceptors.request.use(
  function (config) {
    const token = cookies.get('token') || ''

    const url = new Url(config.url, true)
    url.query.token = token
    config.url = url.toString()
		//给请求的url增加token

    if (!config.data) config.data = {}
    if (config.method === 'post') {
      config.data = qs.stringify(config.data)
      config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'
    }
    return config
  },
  function (error) {
		Toast(error);
    return Promise.reject(error)
  }
)

axios.interceptors.response.use(

  response => {
		//console.log(response);
    let res = response.data;
		if(res && +res.retCode === 0){
			return res.data;
		}
		Toast(res.errMsg);
    return false;
  },(e=>{
		Toast(e.toString());
		return Promise.reject(e)
	})
		
)

export default axios
